package Purchase;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.Random;
import javax.print.CancelablePrintJob;
import javax.swing.JOptionPane;
public class GetRandomNumber extends javax.swing.JFrame implements ActionListener, Runnable {

	public GetRandomNumber() {
		initComponents();
		super.setTitle("36选7");
		int width = Toolkit.getDefaultToolkit().getScreenSize().width;
		int height = Toolkit.getDefaultToolkit().getScreenSize().height;
		super.setSize(400, 350);
		super.setLocation((width - 400) / 2, (height - 350) / 2);
		// 初始化号码
		for (int i = 1; i <= 36; i++) {
			this.getNumberlist().add(i + "");
		}

	}

	private void initComponents() {

		jTextField1 = new javax.swing.JTextField();
		jTextField2 = new javax.swing.JTextField();
		jTextField3 = new javax.swing.JTextField();
		jTextField4 = new javax.swing.JTextField();
		jTextField5 = new javax.swing.JTextField();
		jTextField6 = new javax.swing.JTextField();
		jTextField7 = new javax.swing.JTextField();
		jLabel1 = new javax.swing.JLabel();
		jLabel2 = new javax.swing.JLabel();
		jLabel3 = new javax.swing.JLabel();
		jLabel4 = new javax.swing.JLabel();
		jLabel5 = new javax.swing.JLabel();
		jLabel6 = new javax.swing.JLabel();
		jLabel7 = new javax.swing.JLabel();
		jLabel8 = new javax.swing.JLabel();
		jTextField1.setEditable(false);
		jTextField2.setEditable(false);
		jTextField3.setEditable(false);
		jTextField4.setEditable(false);
		jTextField5.setEditable(false);
		jTextField6.setEditable(false);
		jTextField7.setEditable(false);
		begin = new javax.swing.JButton();
		stop = new javax.swing.JButton();
		cancel = new javax.swing.JButton();
		setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

		jLabel1.setText("    ①");

		jLabel2.setText("    ②");

		jLabel3.setText("    ③");

		jLabel4.setText("    ④");

		jLabel5.setText("    ⑤");

		jLabel6.setText("    ⑥");

		jLabel7.setText("    ⑦");

		jLabel8.setText("36选7抽奖池");

		begin.setText("开始");
		stop.setText("停止");
		cancel.setText("返回");
		cancel.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cancelActionPerformed(evt);
            }
        });
		javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
		getContentPane().setLayout(layout);
		layout.setHorizontalGroup(
				layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING).addGroup(
						javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup().addGap(47, 47, 47)
								.addGroup(layout
										.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING).addGroup(layout
												.createSequentialGroup()
												.addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, 44,
														javax.swing.GroupLayout.PREFERRED_SIZE)
												.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED,
														50, Short.MAX_VALUE)
												.addComponent(jTextField2, javax.swing.GroupLayout.PREFERRED_SIZE, 44,
														javax.swing.GroupLayout.PREFERRED_SIZE))
										.addGroup(layout.createSequentialGroup()
												.addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 44,
														javax.swing.GroupLayout.PREFERRED_SIZE)
												.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED,
														javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
												.addComponent(jLabel2, javax.swing.GroupLayout.PREFERRED_SIZE, 44,
														javax.swing.GroupLayout.PREFERRED_SIZE)))
								.addGap(39, 39, 39)
								.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
										.addComponent(jTextField3, javax.swing.GroupLayout.PREFERRED_SIZE, 44,
												javax.swing.GroupLayout.PREFERRED_SIZE)
										.addComponent(jLabel3, javax.swing.GroupLayout.PREFERRED_SIZE, 44,
												javax.swing.GroupLayout.PREFERRED_SIZE))
								.addGap(42, 42, 42)
								.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
										.addComponent(jLabel4, javax.swing.GroupLayout.PREFERRED_SIZE, 44,
												javax.swing.GroupLayout.PREFERRED_SIZE)
										.addComponent(jTextField4, javax.swing.GroupLayout.PREFERRED_SIZE, 44,
												javax.swing.GroupLayout.PREFERRED_SIZE))
								.addGap(46, 46, 46))
						.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
								.addContainerGap(69, Short.MAX_VALUE)
								.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
										.addGroup(javax.swing.GroupLayout.Alignment.TRAILING,
												layout.createSequentialGroup().addComponent(jLabel8).addGap(165, 165,
														165))
										.addGroup(javax.swing.GroupLayout.Alignment.TRAILING,
												layout.createSequentialGroup().addGroup(layout
														.createParallelGroup(
																javax.swing.GroupLayout.Alignment.TRAILING)
														.addGroup(layout.createSequentialGroup().addGroup(layout
																.createParallelGroup(
																		javax.swing.GroupLayout.Alignment.TRAILING)
																.addComponent(jLabel5,
																		javax.swing.GroupLayout.PREFERRED_SIZE, 44,
																		javax.swing.GroupLayout.PREFERRED_SIZE)
																.addComponent(jTextField5,
																		javax.swing.GroupLayout.PREFERRED_SIZE, 44,
																		javax.swing.GroupLayout.PREFERRED_SIZE))
																.addGap(47, 47, 47))
														.addGroup(layout.createSequentialGroup().addComponent(begin)
																.addGap(58, 58, 58)))
														.addGroup(layout
																.createParallelGroup(
																		javax.swing.GroupLayout.Alignment.LEADING)
																.addGroup(layout.createSequentialGroup()
																		.addComponent(jTextField6,
																				javax.swing.GroupLayout.PREFERRED_SIZE,
																				44,
																				javax.swing.GroupLayout.PREFERRED_SIZE)
																		.addGap(41, 41, 41)
																		.addComponent(jTextField7,
																				javax.swing.GroupLayout.PREFERRED_SIZE,
																				44,
																				javax.swing.GroupLayout.PREFERRED_SIZE)
																		.addGap(87, 87, 87))
																.addGroup(layout.createSequentialGroup()
																		.addGroup(layout
																				.createParallelGroup(
																						javax.swing.GroupLayout.Alignment.LEADING)
																				.addGroup(layout.createSequentialGroup()
																						.addGap(13, 13, 13)
																						.addComponent(jLabel6,
																								javax.swing.GroupLayout.PREFERRED_SIZE,
																								44,
																								javax.swing.GroupLayout.PREFERRED_SIZE))
																				.addComponent(stop))
																		.addGroup(layout
																				.createParallelGroup(
																						javax.swing.GroupLayout.Alignment.LEADING)
																				.addGroup(layout.createSequentialGroup()
																						.addPreferredGap(
																								javax.swing.LayoutStyle.ComponentPlacement.RELATED,
																								javax.swing.GroupLayout.DEFAULT_SIZE,
																								Short.MAX_VALUE)
																						.addComponent(jLabel7,
																								javax.swing.GroupLayout.PREFERRED_SIZE,
																								44,
																								javax.swing.GroupLayout.PREFERRED_SIZE)
																						.addGap(87, 87, 87))
																				.addGroup(layout.createSequentialGroup()
																						.addGap(52, 52, 52)
																						.addComponent(cancel)
																						.addContainerGap(
																								javax.swing.GroupLayout.DEFAULT_SIZE,
																								Short.MAX_VALUE)))))))));
		layout.setVerticalGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
				.addGroup(layout.createSequentialGroup().addGap(24, 24, 24)
						.addComponent(jLabel8, javax.swing.GroupLayout.PREFERRED_SIZE, 30,
								javax.swing.GroupLayout.PREFERRED_SIZE)
						.addGap(18, 18, 18)
						.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
								.addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, 36,
										javax.swing.GroupLayout.PREFERRED_SIZE)
								.addComponent(jTextField2, javax.swing.GroupLayout.PREFERRED_SIZE, 36,
										javax.swing.GroupLayout.PREFERRED_SIZE)
								.addComponent(jTextField3, javax.swing.GroupLayout.PREFERRED_SIZE, 36,
										javax.swing.GroupLayout.PREFERRED_SIZE)
								.addComponent(jTextField4, javax.swing.GroupLayout.PREFERRED_SIZE, 36,
										javax.swing.GroupLayout.PREFERRED_SIZE))
						.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
						.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
								.addComponent(jLabel1)
								.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
										.addComponent(jLabel3).addComponent(jLabel2).addComponent(jLabel4)))
						.addGap(18, 18, 18)
						.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
								.addComponent(jTextField7, javax.swing.GroupLayout.PREFERRED_SIZE, 36,
										javax.swing.GroupLayout.PREFERRED_SIZE)
								.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
										.addComponent(jTextField6, javax.swing.GroupLayout.PREFERRED_SIZE, 36,
												javax.swing.GroupLayout.PREFERRED_SIZE)
										.addComponent(jTextField5, javax.swing.GroupLayout.PREFERRED_SIZE, 36,
												javax.swing.GroupLayout.PREFERRED_SIZE)))
						.addGap(18, 18, 18)
						.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
								.addComponent(jLabel5).addComponent(jLabel6).addComponent(jLabel7))
						.addGap(18, 18, 18)
						.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
								.addComponent(cancel, javax.swing.GroupLayout.DEFAULT_SIZE,
										javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
								.addComponent(begin, javax.swing.GroupLayout.DEFAULT_SIZE, 31, Short.MAX_VALUE)
								.addComponent(stop, javax.swing.GroupLayout.DEFAULT_SIZE,
										javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
						.addContainerGap(35, Short.MAX_VALUE)));
		begin.addActionListener(this);
		stop.addActionListener(this);
		pack();
	}

	public void actionPerformed(ActionEvent e) {
		// TODO Auto-generated method stub
		Object obj = e.getSource();
		if (obj == begin) {
			synchronized (this) {
				flag = true;
				notify(); // 唤醒线程

			}
		}
		if (obj == stop) {
			synchronized (this) {
				flag = false;
			}
		}
	}

	public void run() {
		while (count < 8) {
			int length = numberlist.size();
			if (count == 1) {
				while (true) {
					try {

						if (!flag) {
							if (jTextField1.getText().length() != 0) {
								shotnum.add(Integer.parseInt(jTextField1.getText()));
								this.getNumberlist().remove(jTextField1.getText());

								break;
							}
							synchronized (this) {
								wait();
							}
						}

						Random myRandomName = new Random();
						int RN = myRandomName.nextInt(length);

						jTextField1.setText(numberlist.get(RN));
						Thread.sleep(100);

					} catch (InterruptedException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
				}
				count++;
			} else if (count == 2) {
				while (true) {
					try {
						if (!flag) {
							if (jTextField2.getText().length() != 0) {

								shotnum.add(Integer.parseInt(jTextField2.getText()));
								this.getNumberlist().remove(jTextField2.getText());
								break;
							}
							synchronized (this) {
								wait();

							}

						}
						Random myRandomName = new Random();
						int RN = myRandomName.nextInt(length);
						jTextField2.setText(numberlist.get(RN));

						Thread.sleep(100);
					} catch (InterruptedException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
				}
				count++;
			} else if (count == 3) {
				while (true) {
					try {
						if (!flag) {
							if (jTextField3.getText().length() != 0) {

								shotnum.add(Integer.parseInt(jTextField3.getText()));
								this.getNumberlist().remove(jTextField3.getText());
								break;
							}
							synchronized (this) {
								this.wait();

							}
						}
						Random myRandomName = new Random();
						int RN = myRandomName.nextInt(length);
						jTextField3.setText(numberlist.get(RN));

						Thread.sleep(100);
					} catch (InterruptedException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
				}
				count++;
			} else if (count == 4) {
				while (true) {
					try {
						if (!flag) {
							if (jTextField4.getText().length() != 0) {

								shotnum.add(Integer.parseInt(jTextField4.getText()));
								this.getNumberlist().remove(jTextField4.getText());
								break;
							}
							synchronized (this) {
								this.wait();

							}
						}
						Random myRandomName = new Random();
						int RN = myRandomName.nextInt(length);
						jTextField4.setText(numberlist.get(RN).toString());
						Thread.sleep(100);
					} catch (InterruptedException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
				}
				count++;
			} else if (count == 5) {
				while (true) {
					try {
						if (!flag) {
							if (jTextField5.getText().length() != 0) {

								shotnum.add(Integer.parseInt(jTextField5.getText()));
								this.getNumberlist().remove(jTextField5.getText());
								break;
							}
							synchronized (this) {
								this.wait();

							}
						}
						Random myRandomName = new Random();
						int RN = myRandomName.nextInt(length);
						jTextField5.setText(numberlist.get(RN).toString());
						Thread.sleep(100);
					} catch (InterruptedException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
				}
				count++;
			} else if (count == 6) {
				while (true) {
					try {
						if (!flag) {
							if (jTextField6.getText().length() != 0) {

								shotnum.add(Integer.parseInt(jTextField6.getText()));
								this.getNumberlist().remove(jTextField6.getText());
								break;
							}
							synchronized (this) {
								this.wait();

							}
						}
						Random myRandomName = new Random();
						int RN = myRandomName.nextInt(length);
						jTextField6.setText(numberlist.get(RN));
						Thread.sleep(100);
					} catch (InterruptedException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
				}
				count++;
			} else if (count == 7) {
				while (true) {
					try {
						if (!flag) {
							if (jTextField7.getText().length() != 0) {

								shotnum.add(Integer.parseInt(jTextField7.getText()));
								this.getNumberlist().remove(jTextField7.getText());
								break;
							}
							synchronized (this) {
								this.wait();

							}
						}
						Random myRandomName = new Random();
						int RN = myRandomName.nextInt(length);
						jTextField7.setText(numberlist.get(RN));
						Thread.sleep(100);
					} catch (InterruptedException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
				}
				count++;
			}
			length = numberlist.size();
		}
//		if (count == 8) {
//			System.out.println("count=" + count);
//			return;
//		}

	}
    private void cancelActionPerformed(java.awt.event.ActionEvent evt) {                                       
        // TODO add your handling code here:
        this.setVisible(false);
    
        
}   
	private static void addwinnumber() throws ClassNotFoundException {
		String strSql = "insert into winmes(num1,num2,num3,num4,num5,num6,num7) values(?,?,?,?,?,?,?)";
		Connection con = null;
		PreparedStatement prepare = null;
		try {
			Class.forName(driverName); // 加载MySql的驱动程序
			con = DriverManager.getConnection(url, userName, password);
			con.setAutoCommit(false);
			prepare = con.prepareStatement(strSql);
			Statement statement = con.createStatement();
			statement.executeUpdate("truncate table winmes");// 每次都要先清空
			for (int i = 0; i < 7; i++) {
				//System.out.println(shotnum.get(i));
				prepare.setInt(i + 1, shotnum.get(i));
			}
			prepare.addBatch();
			prepare.executeBatch();
			con.commit();
		} catch (SQLException sqlE) {
			sqlE.printStackTrace();
		}

	}

	public static void main(String args[]) throws InterruptedException, ClassNotFoundException {

		GetRandomNumber rn = new GetRandomNumber();
		rn.setVisible(true);
		Thread t1 = new Thread(rn);
		t1.start();
		t1.join();
		addwinnumber();
        JOptionPane.showMessageDialog(null,"已将数据存入数据库中");
		
	}
	
	ArrayList<String> numberlist = new ArrayList<String>();
	private boolean flag = false;
	public ArrayList<String> getNumberlist() {
		return numberlist;
	}

	public static int count = 1;

	public void setNumberlist(ArrayList<String> numberlist) {
		this.numberlist = numberlist;
	}

	public static String url = "jdbc:mysql://localhost:3306/message?useUnicode=true&characterEncoding=GBK&useSSL=false";
	public static String userName = "root";
	public static String password = "552364871";
	public static String driverName = "com.mysql.jdbc.Driver";
	public static Connection connection = null;
	public static Statement statement = null;
	public static ResultSet resultSet = null;
	private static ArrayList<Integer> shotnum = new ArrayList<Integer>();
	private javax.swing.JButton begin;
	private javax.swing.JButton cancel;
	private javax.swing.JLabel jLabel1;
	private javax.swing.JLabel jLabel2;
	private javax.swing.JLabel jLabel3;
	private javax.swing.JLabel jLabel4;
	private javax.swing.JLabel jLabel5;
	private javax.swing.JLabel jLabel6;
	private javax.swing.JLabel jLabel7;
	private javax.swing.JLabel jLabel8;
	private javax.swing.JTextField jTextField1;
	private javax.swing.JTextField jTextField2;
	private javax.swing.JTextField jTextField3;
	private javax.swing.JTextField jTextField4;
	private javax.swing.JTextField jTextField5;
	private javax.swing.JTextField jTextField6;
	private javax.swing.JTextField jTextField7;
 	private javax.swing.JButton stop;

}
